package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Projectapply;
import com.example.mapper.RecordMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class RecordService {


    @Autowired
    private RecordMapper recordMapper;

    /**
     * 新增
     */
    public void add(Projectapply projectapply) {
        projectapply.setTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
        String role = TokenUtils.getCurrentUser().getRole();
        if(Objects.equals(role, "HELPER")) {
            projectapply.setUserId(null);
        }
        Integer id = TokenUtils.getCurrentUser().getId();
        projectapply.setRole(role);
        projectapply.setHelperId(id);

        recordMapper.insert(projectapply);
    }


    /**
     * 查询所有
     */
    public List<Projectapply> selectAll(Projectapply projectapply) {
        projectapply.setHelperId(TokenUtils.getCurrentUser().getId());
        return recordMapper.selectAll(projectapply);
    }
    /**
     * 分页查询
     */
    public PageInfo<Projectapply> selectPage(Projectapply projectapply, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String role = TokenUtils.getCurrentUser().getRole();
        if(Objects.equals(role, "HELPER")) {
            projectapply.setHelperId(TokenUtils.getCurrentUser().getId());
            projectapply.setRole(role);
            projectapply.setUserId(null);
        }else if ((Objects.equals(role, "USER"))){
            projectapply.setUserId(TokenUtils.getCurrentUser().getId());
            projectapply.setRole(role);
            projectapply.setHelperId(null);
        }else {
            projectapply.setUserId(null);
        }
        List<Projectapply> list = recordMapper.selectAll(projectapply);
        System.out.println(list);
        return PageInfo.of(list);
    }
}
